package com.huawei.struts.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.st.model.LjwUserT;
import com.st.model.UserT;

public class RightFilter implements Filter {

	 /*
     * HttpServletRequest对象
     */
    private HttpServletRequest thisRequest;
    /**
     * HttpServletResponse对象
     */
    private HttpServletResponse thisResponse;
    /**
     * The default character encoding to set for requests that pass through
     * this filter.
     */
    protected String encoding = null;
    /**
     * The filter configuration object we are associated with.  If this value
     * is null, this filter instance is not currently configured.
     */
    protected FilterConfig filterConfig = null;
    /**
     * Should a character encoding specified by the client be ignored?
     */
    protected boolean ignore = true;
    /**
     * init
     */
    public void init(FilterConfig filterConfig) throws ServletException {
    	

    }
    /**
     * destroy
     */
    public void destroy() {
        
    }
    /**
     * doFilter
     */
    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
        // Conditionally select and set the character encoding to be used
    	
    	HttpServletRequest  thisRequest = (HttpServletRequest)request;
    	HttpServletResponse thisResponse = (HttpServletResponse)response;
    	thisRequest.setCharacterEncoding("UTF-8");
    	thisResponse.setCharacterEncoding("UTF-8");
    	//thisResponse.setCharacterEncoding("UTF-8");
       
        String path = thisRequest.getRequestURI()+"?"+thisRequest.getQueryString();
        System.out.println(path);
    	//如果不是登陆url
        if(!path.contains("user.do?method=loginVlidate") && !path.contains("index.jsp") && !path.contains("user.do?method=login")  &&  !path.contains("null")){ //如果是登录操作，就不过滤权限
        	LjwUserT userBean = (LjwUserT)thisRequest.getSession().getAttribute("user");
            
            if(null == userBean){ 
            	HttpSession session=(HttpSession)thisRequest.getSession();
            	session.setAttribute("path", path);
                thisResponse.sendRedirect("./index.jsp");   
            }
            else
            {
            	 chain.doFilter(request, response);
            }
        }
        else//防止登陆后 url地址为index.jsp,login.do?method=login,null,login.do?method=loginVlidate
        {
        	LjwUserT userBean = (LjwUserT)thisRequest.getSession().getAttribute("user");
        	if(null != userBean){ 
        		 
                thisResponse.sendRedirect("user.do?method=goHome");
                 
            }
        	else
        	{
        		chain.doFilter(request, response);
        	}
            
        }
       
    }
   


}
